<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('仓库类单据列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <p>单据号：</p>
                                <input type="text" name="wReceiptNumber"/>
                            </li>
                            <li>
                                <p>单据类型：</p>
                                <select name="wReceiptType" th:with="type=${@dict.getType('receipt_type')}" id="wReceiptType">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <p>工单：</p>
                                <input type="text" name="wWorkOrder"/>
                            </li>
                            <li class="select-time">
                                <p>创建单据日期：</p>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginWCreateDate]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endWCreateDate]"/>
                            </li>
                            <li hidden>
                                <p>补充说明：</p>
                                <input type="text" name="wRemarks"/>
                            </li>
                            <li hidden>
                                <p>创建单据人id：</p>
                                <input type="text" name="wCreatePersonId"/>
                            </li>
                            <li>
                                <p>创建人：</p>
                                <input type="text" name="wCreatePersonName"/>
                            </li>
                            <li>
                                <p>物料类型：</p>
                                <select name="materialType" th:with="type=${@dict.getType('materials_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <p>审核状态：</p>
                                <!--                                <input type="text" name="isCheck"/>-->
                                <select name="isCheck" th:with="type=${@dict.getType('check_types')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="receipt:receipt:add">
                    <i class="fa fa-plus"></i> 新增仓库类单据
                </a>
                <a class="btn btn-primary single disabled" onclick="isCheckPass(3)" shiro:hasPermission="receipt:receipt:check">
                    <i class="fa fa-edit"></i> 审核通过
                </a>
                <a class="btn btn-primary single disabled" style="background-color:red" onclick="isCheckPass(4)" shiro:hasPermission="receipt:receipt:check">
                    <i class="fa fa-edit"></i> 审核不通过
                </a>
                <a class="btn btn-primary single disabled" onclick="edit()" shiro:hasPermission="receipt:receipt:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="receipt:receipt:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="receipt:receipt:export">
                    <i class="fa fa-download"></i> 导出
                 </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('receipt:receipt:edit')}]];
        var removeFlag = [[${@permission.hasPermi('receipt:receipt:remove')}]];
        var checkFlag = [[${@permission.hasPermi('receipt:receipt:check')}]];
        var prefix = ctx + "receipt/receipt";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "仓库类单据",
                columns: [{
                    checkbox: true
                },
                {
                    field : 'wReceiptId', 
                    title : '仓库类单据ID',
                    visible: false
                },
                {
                    title: "序号",
                    formatter: function (value, row, index) {
                        return $.table.serialNumber(index);
                    }
                },
                {
                    field : 'wReceiptNumber', 
                    title : '单据号'
                },
                {
                    field : 'wReceiptType', 
                    title : '单据类型',
                    formatter:getReceiptType,
                },
                {
                    field : 'wWorkOrder', 
                    title : '工单(生产类单据才会有工单)'
                },
                {
                    field : 'wCreateDate', 
                    title : '创建单据日期'
                },
                {
                    field : 'materialType',
                    title : '物料类型',
                    formatter:getMaterialType
                },
                {
                    field : 'qty',
                    title : '数量'
                },
                {
                    field : 'wRemarks', 
                    title : '补充说明'
                },
                {
                    field : 'wCreatePersonId', 
                    title : '创建单据人id',
                    visible: false
                },
                {
                    field : 'wCreatePersonName', 
                    title : '创建人名字'
                },
                {
                    field : 'isCheck',
                    title : '是否审核',
                    formatter:isCheck,
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if(row.isCheck == 1){
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.wReceiptId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        }
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.wReceiptId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        // actions.push('<a class="btn btn-success btn-xs ' + checkFlag + '" href="javascript:void(0)" onclick="isCheckPassRow(3)"><i class="fa fa-edit"></i>审核通过</a>');
                        // actions.push('<a class="btn btn-danger btn-xs ' + checkFlag + '" href="javascript:void(0)" onclick="isCheckPassRow(4)"><i class="fa fa-edit"></i>审核不通过</a>');
                        return actions.join('');
                    }
                }],
                onLoadSuccess: function (row) {
                    getTdValue(row);
                }
            };
            $.table.init(options);
        });
        function getReceiptType(value, row, index) {
            var typeValue = [[${@dict.getType('receipt_type')}]];
            // typeValue.forEach(function (element) {
            //     console.log(element.dictSort)a
            //     if(element.dictSort == value){
            //         return element.dictLabel;
            //     }
            // })//达不到想要的跳出循环的效果
            console.log(typeValue)
            for(var i = 0;i<typeValue.length;i++){
                if(typeValue[i].dictSort == value){
                    return typeValue[i].dictLabel;
                    break;
                }
            }
        };
        function isCheck(value, row, index) {
            var val = [[${@dict.getType('check_types')}]];
            for(var i = 0;i<val.length;i++){
                if(val[i].dictSort == value){
                    console.log(val[i].dictLabel)
                    return val[i].dictLabel;
                    break;
                }
            }
        };
        function getMaterialType(value, row, index) {
            var val = [[${@dict.getType('materials_type')}]];
            for(var i = 0;i<val.length;i++){
                if(val[i].dictSort == value){
                    console.log(val[i].dictLabel)
                    return val[i].dictLabel;
                    break;
                }
            }
        }
        function isCheckPass(status) {
            var id=$("#bootstrap-table").bootstrapTable('getSelections')[0].wReceiptId;
            var ck=$("#bootstrap-table").bootstrapTable('getSelections')[0].isCheck;
            console.log(ck);
            if(null==id || "" == id){
                $.modal.alertWarning("请选择一条记录");
                return;
            }
            if(1 != ck){
                $.modal.alertWarning("不允许修改已经审核单据");
                return;
            }
            console.log(status);
            $.ajax({
                url: prefix+"/check",
                type:"POST",
                data:{
                    id:id,
                    status:status
                },
                success: function (result) {
                    if (result.code == web_status.SUCCESS) {
                        $.modal.closeAll();
                        $.modal.alertSuccess(result.msg);
                        $.table.refresh();
                    } else if (result.code == web_status.WARNING) {
                        layer.close(index);
                        $.modal.enable();
                        $.modal.alertWarning(result.msg)
                    } else {
                        layer.close(index);
                        $.modal.enable();
                        $.modal.alertError(result.msg);
                    }
                }
            })
        };

        // 修改信息
        // edit: function(id) {
        //     table.set();
        //     if($.common.isEmpty(id) && table.options.type == table_type.bootstrapTreeTable) {
        //         var row = $("#" + table.options.id).bootstrapTreeTable('getSelections')[0];
        //         if ($.common.isEmpty(row)) {
        //             $.modal.alertWarning("请至少选择一条记录");
        //             return;
        //         }
        //         var url = table.options.updateUrl.replace("{id}", row[table.options.uniqueId]);
        //         $.modal.open("修改" + table.options.modalName, url);
        //     } else {
        //         $.modal.open("修改" + table.options.modalName, $.operate.editUrl(id));
        //     }
        // };

        //当远程数据被加载完成后触发
        function getTdValue(row) {
            var tableId = document.getElementById("bootstrap-table");
            // var tableId = $("#bootstrap-table");
            var y = 0;
            for (var i = 0; i < row.rows.length; i++) {
                //将已标记的数据改变背景颜色
                y++;
                if (row.rows[i].isCheck == 1) {//待审核
                    tableId.rows[y].setAttribute("style", "background-color:#CAE1FF");
                }
                if (row.rows[i].isCheck == 3) {//通过
                    tableId.rows[y].setAttribute("style", "background-color:#66CD00");
                }
                if (row.rows[i].isCheck == 4) {//不通过
                    tableId.rows[y].setAttribute("style", "background-color:#FF7256");
                }
            }
        }
        function edit() {
            var ck=$("#bootstrap-table").bootstrapTable('getSelections')[0].isCheck;
            if(1 !=ck){
                $.modal.alertWarning("不允许修改已经审核单据");
                return;
            }
            return $.operate.edit();
        }
    </script>
</body>
</html>